﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class bookOrderService : IbookOrderService
    {
        private IBookOrderDal _bookOrder;
        private IMapper _mapper;
        public bookOrderService(IBookOrderDal bookOrder, IMapper mapper)
        {
            _bookOrder = bookOrder;
            _mapper = mapper;
        }

        public async Task<int> AddbookOrderAsync(int infoId, int number, decimal price)
        {
            return await _bookOrder.AddAsync(new Bookorder 
            {
                InfoId = infoId,
                Number = number,
                Price = price
            });
        }

        public async  Task<int> DeletebookOrderAsync(int id)
        {
            var data = await _bookOrder.QueryAsync(id);
            return data == null ? -1 : await _bookOrder.DeleteAsync(data);
        }

        public async  Task<int> EditbookOrderAsync(int id, int infoId, int number, decimal price)
        {
            var data = await _bookOrder.QueryAsync(id);
            if (data == null)
                return -1;
            data.Number = number;
            data.Price = price;
            return await _bookOrder.EditAsync(data);
        }

        public async  Task<List<BookOrderDto>> QuerybookOrderAllAsync()
        {
            var data = await _bookOrder.QueryAsync();
            return _mapper.Map<List<Bookorder>, List<BookOrderDto>>(data);
        }

        public async  Task<BookOrderDto> QuerybookOrderByIdAsync(int id)
        {
            var data = await _bookOrder.QueryAsync(id);
            return _mapper.Map<Bookorder, BookOrderDto>(data);
        }
    }
}
